package com.hiwhitley.himei.module.video;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hiwhitley.himei.R;
import com.hiwhitley.himei.entity.FindMore;
import com.hiwhitley.himei.widget.glide.GlideImageView;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.drakeet.multitype.ItemViewProvider;

/**
 * Created by hiwhitley on 17-2-26.
 */
public class FindMoreViewProvider
        extends ItemViewProvider<FindMore, FindMoreViewProvider.ViewHolder> {

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_find_more, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull FindMore findMore) {
        holder.mGridTv.setText(findMore.getName());
        holder.mGridIv.loadNetImage(findMore.getBgPicture(), R.drawable.bg_holder_brown);
        holder.mGridIvBg.setOnClickListener(v -> {
            Context context = holder.mGridIvBg.getContext();
            Intent intent = FindMoreInfoActivity.newIntent(context, findMore.getName());
            ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(
                    (Activity) context, holder.mGridIvBg, FindMoreInfoActivity.TRAN_TAG);
            try {
                ActivityCompat.startActivity(context, intent, optionsCompat.toBundle());
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
                context.startActivity(intent);
            }
        });
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.grid_iv)
        GlideImageView mGridIv;
        @BindView(R.id.grid_tv)
        TextView mGridTv;
        @BindView(R.id.grid_iv_bg)
        ImageView mGridIvBg;

        ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}